package com.boshao.classschedule.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.utils.ViewHolder;

/**
 * 选择星期的dialog UI
 * @author boshao
 *
 */
public class WeeksDialogFragment extends DialogFragment implements OnClickListener,OnItemClickListener{
	public static final String TAG = "weeks_dialog_fragment";
	private View mRootView;
	private GridView mGridView;
	private TextView mTvCancel,mTvSave;
	private AddOrModifyClassFragment mFragment;
	private DialogAdapter mDialogAdapter;
	private static final int COUNT = 18;
	private Map<Integer,Boolean> mMap = new HashMap<>();
	private View mAll,mDouble,mSingle;
	private List<Integer> list = new ArrayList<>();
	private static final int ALL = 0;
	private static final int SINGLE = 1;
	private static final int DOUBLE = 2;
	
	private int flag;
	private int p;
	private StringBuilder sb = new StringBuilder();
	
	public static WeeksDialogFragment newInstance(AddOrModifyClassFragment frament){
		WeeksDialogFragment fragment = new WeeksDialogFragment();
		fragment.setClassScheduldeFragment(frament);
		return fragment;
	}
	
	public void setClassScheduldeFragment(AddOrModifyClassFragment frament){
		mFragment = frament;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE,0);
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.dialogfragment_repeat, container,false);
		mGridView = (GridView) mRootView.findViewById(R.id.gv_week);
		mRootView.findViewById(R.id.tv_repeat).setVisibility(View.GONE);
		mDialogAdapter = new DialogAdapter();
		mGridView.setAdapter(mDialogAdapter);
//		mGridView.setOnItemClickListener(this);
		mTvCancel = (TextView) mRootView.findViewById(R.id.tv_cancel);
		mTvCancel.setOnClickListener(this);
		mTvSave = (TextView) mRootView.findViewById(R.id.tv_save);
		mTvSave.setOnClickListener(this);
		mAll = mRootView.findViewById(R.id.rl_all);
		mAll.setOnClickListener(this);
		mDouble = mRootView.findViewById(R.id.rl_single);
		mDouble.setOnClickListener(this);
		mSingle = mRootView.findViewById(R.id.rl_double);
		mSingle.setOnClickListener(this);
		return mRootView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_save:
				save();
				break;
			case R.id.tv_cancel:
				dismiss();
				break;
			case R.id.rl_all:
				clearMap();
				for (int i = 0; i < COUNT; i++) {
					mMap.put(i,true);
				}
				mDialogAdapter.notifyDataSetChanged();
				flag = ALL;
				break;
			case R.id.rl_double:
				clearMap();
				for (int i = 0; i < COUNT; i++) {
					if(i % 2 != 0){
						mMap.put(i,true);
					}
				}
				mDialogAdapter.notifyDataSetChanged();
				flag = DOUBLE;
				
				break;
			case R.id.rl_single:
				clearMap();
				for (int i = 0; i < COUNT; i++) {
					if(i % 2 == 0){
						mMap.put(i,true);
					}
				}
				mDialogAdapter.notifyDataSetChanged();
				flag = SINGLE;
				
				break;
		}
	}
	
	private void save() {
		if(flag == ALL){
			mFragment.mTvWeek.setText("1-18周");
		}else if(flag == SINGLE){
			mFragment.mTvWeek.setText("单周");
		}else{
			mFragment.mTvWeek.setText("双周");
		}
		dismiss();
	}

	
	private void clearMap() {
		for (int i = 0; i < COUNT; i++) {
			mMap.put(i,false);
		}
	}
	
	class DialogAdapter extends BaseAdapter{
		public DialogAdapter() {
			clearMap();
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.gridview_weeks,parent, false);
			}
			TextView week = ViewHolder.get(convertView,R.id.tv_week);
			ImageView iv = ViewHolder.get(convertView,R.id.iv_week);
			if(mMap.get(position)){
				iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_circle_shap));
			}else{
				iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.gray_circle_shap));
			}
			week.setText(String.valueOf(position+1));
			convertView.setClickable(true);
			return convertView;
		}			
			
		@Override
		public long getItemId(int position) {
			return position;
		}			
		@Override
		public Object getItem(int position) {
			return mMap.get(position);
		}			
		@Override
		public int getCount() {
			return COUNT;
		}
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
	}
}
